package com.itc.bi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bi.dto.ColumnDataDto;
import com.itc.bi.dto.DataSetCreateDto;
import com.itc.bi.dto.DataSetInfoDto;
import com.itc.bi.dto.DataSetListDto;
import com.itc.bi.entity.*;
import com.itc.bi.mapper.*;
import com.itc.bi.query.TableQuery;
import com.itc.bi.service.*;
import com.itc.bi.vo.bigScreenVO.ProDirDataSetVO;
import com.itc.common.core.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* @author hecai
* @description 针对表【tb_direct_data_set(数据集表)】的数据库操作Service实现
* @createDate 2024-06-14 17:25:39
*/
@Service
public class DirectDataSetServiceImpl extends ServiceImpl<DirectDataSetMapper, DirectDataSetEntity> implements DirectDataSetService{

    @Autowired
    TableService tableService;
    @Autowired
    DirectDataSetMapper directDataSetMapper;
    @Autowired
    DataSynrecordMapper dataSynrecordMapper;
    @Autowired
    private BigScreenImageMapper bigScreenImageMapper;
    @Autowired
    ClickHouseJDBCService clickHouseJDBCService;
    @Autowired
    DataSetColumnService dataSetColumnService;
    @Autowired
    DataProcessInfoService dataProcessInfoService;
    @Autowired
    DirectoryMapper directoryMapper;
    @Autowired
    DataSetParameterMapper dataSetParameterMapper;


    public List<TableEntity> getTables(Long dsId, Long dtId){
        TableQuery query = new TableQuery();
        query.setDatasourceId(dsId);
        //获取数据源下的所有表
        List<TableEntity> list = tableService.list(query);
        if(null != list && list.size()>0){
            //目录下数据源已经创建过数据集的表
            List<String> oldList = directDataSetMapper.getTableNameByDsIdAndDtId(dsId,dtId);
            if(null != oldList && oldList.size()>0){
                LambdaQueryWrapper<DataSynrecordEntity> wq = Wrappers.lambdaQuery();
                list.stream().forEach(item -> {
                    if(oldList.contains(item.getTableName().toLowerCase())){
                        //设置同步时间
                        wq.clear();
                        wq.eq(DataSynrecordEntity::getTableId,item.getId());
                        wq.eq(DataSynrecordEntity::getDirId,dtId);
                        List<DataSynrecordEntity> synList = dataSynrecordMapper.selectList(wq);
                        if(null != synList && synList.size()>0){
                            item.setSynTime(synList.get(0).getUpdateTime());
                        }
                        //设置选择状态
                        item.setSelect(true);
                    }
                });
            }
        }
        return list;
    }

    @Override
    public List<String> columnData(ColumnDataDto dto){
        String tableName=null;

        if(null != dto.getProcessInfoId()){
            DataProcessInfoEntity info = dataProcessInfoService.getById(dto.getProcessInfoId());
            if(null == info){
                throw new ServiceException("数据集不存在");
            }
            Integer orderNum = info.getOrderNum();
            tableName = dataProcessInfoService.getTableName(dto.getId(),orderNum);
            if(StringUtils.isBlank(tableName)){
                throw new ServiceException("数据集不存在");
            }
        }else{
            ProDirDataSetVO vo = bigScreenImageMapper.getProDirDataSetById(dto.getId());
            if(null == vo){
                tableName = dataProcessInfoService.getTableName(dto.getId(),null);
                if(StringUtils.isBlank(tableName)){
                    throw new ServiceException("数据集不存在");
                }
            }else{
                tableName = vo.getTableName();
            }
        }
        return clickHouseJDBCService.getColumnListByNameAndCol(tableName,dto.getColumnName());
    }
    @Override
    public DataSetInfoDto dataInfo(Long id){
        DataSetInfoDto dto = new DataSetInfoDto();
        DirectDataSetEntity dataSet = getById(id);
        if(null != dataSet){
            dto.setCreateBy(dataSet.getCreateBy());
            dto.setCreateTime(dataSet.getCreateTime());
            dto.setUpdateBy(dataSet.getUpdateBy());
            dto.setUpdateTime(dataSet.getUpdateTime());
            dto.setColumnNum(dataSetColumnService.count(Wrappers.lambdaQuery(DataSetColumnEntity.class).eq(DataSetColumnEntity::getDataSetId,id)));
        }
        return dto;
    }
    @Override
    public List<DataSetListDto> dataSetList(){
        return directDataSetMapper.dataSetList();
    }

    @Override
    public DataSetCreateDto info(Long id){
        DirectDataSetEntity old = super.getById(id);
        if(null == old){
            throw new ServiceException("记录不存在");
        }
        DirectoryEntity directory = directoryMapper.selectById(old.getId());


        DataSetCreateDto dto = new DataSetCreateDto();
        dto.setId(id);
        dto.setType(old.getType());
        dto.setName(directory.getName());
        dto.setDtId(directory.getPid());
        dto.setDsId(old.getDsId());
        dto.setCode(old.getCodeScript());

        List<DataSetParameterEntity> params = dataSetParameterMapper.selectList(Wrappers.lambdaQuery(DataSetParameterEntity.class).eq(DataSetParameterEntity::getDdsId, id));

        dto.setParams(params);

        DataSynrecordEntity syn = dataSynrecordMapper.selectOne(Wrappers.lambdaQuery(DataSynrecordEntity.class).eq(DataSynrecordEntity::getDtId, id));
        if(null != syn){
            dto.setCron(syn.getCron());
        }

        return dto;
    }
}




